class PeerPricesController < ApplicationController
  before_filter :authorize
  
  def index
    
  end
  
  def quoted_enterprises
    @enterprises = Enterprise.where('id in (?)', PeerPrice.select('enterprise_id').where('released_on >= ?',4.days.ago).each_with_object([]){ |peer_price,enterprise_ids| enterprise_ids << peer_price.enterprise_id if not enterprise_ids.include?(peer_price.enterprise_id) })
  end

  def enterprise
    params[:type_name] ||= 'cpu'
    params[:released_on] ||= Date.today
    params[:stock_situation] ||= 'default'
    params[:status] ||= 'default'
    @enterprise = Enterprise.find(params[:enterprise_id])
    @peer_prices = PeerPrice.where(:enterprise_id => @enterprise.id, :released_on => params[:released_on]).try(params[:type_name]).try(params[:stock_situation]).try(params[:status])
    # 浏览记录
    Visit.create(
      :type_name => params[:type_name],
      :enterprise_id => params[:enterprise_id],
      :visit_enterprise_id => Enterprise.current,
      :visitor_id => User.current
    ) if Enterprise.current != @enterprise
  end

  def new
    @peer_price = PeerPrice.new :type_name => params[:type_name],
      :released_on => Date.today
  end

  def create
    @peer_price = PeerPrice.new params[:peer_price]
    if @peer_price.save
      flash[:notice] = '保存成功！'
    else
      render :new
    end
  end
  
  def show_peer_price
    @peer_price = PeerPrice.find(params[:id])
  end
  
  def edit_peer_price
    @peer_price = PeerPrice.find(params[:id])
  end
  
  def update_peer_price    
    @peer_price = PeerPrice.find(params[:id])
    params["peer_price_#{@peer_price.id}"][:peer_price] = instance_eval(params["peer_price_#{@peer_price.id}"][:peer_price])
    render :edit_peer_price if not @peer_price.update_attributes(params["peer_price_#{@peer_price.id}"])
  end
  
  def title_for_peer_price
    
  end
    
  def show_trade_price
    @peer_price = PeerPrice.find(params[:id])
  end
  
  def edit_trade_price
    @peer_price = PeerPrice.find(params[:id])
  end
  
  def update_trade_price    
    @peer_price = PeerPrice.find(params[:id])
    params["peer_price_#{@peer_price.id}"][:trade_price] = instance_eval(params["peer_price_#{@peer_price.id}"][:trade_price])
    render :edit_trade_price if not @peer_price.update_attributes(params["peer_price_#{@peer_price.id}"])
  end
  
  def show_retail_price
    @peer_price = PeerPrice.find(params[:id])
  end
  
  def edit_retail_price
    @peer_price = PeerPrice.find(params[:id])
  end
  
  def update_retail_price    
    @peer_price = PeerPrice.find(params[:id])
    params["peer_price_#{@peer_price.id}"][:retail_price] = instance_eval(params["peer_price_#{@peer_price.id}"][:retail_price])
    render :edit_retail_price if not @peer_price.update_attributes(params["peer_price_#{@peer_price.id}"])
  end
  
  def show_name
    @peer_price = PeerPrice.find(params[:id])
  end
  
  def edit_name
    @peer_price = PeerPrice.find(params[:id])
  end
  
  def update_name    
    @peer_price = PeerPrice.find(params[:id])
    render :edit_name if not @peer_price.update_attributes(params["peer_price_#{@peer_price.id}"])
  end
  
  def show_model_no
    @peer_price = PeerPrice.find(params[:id])
  end
  
  def edit_model_no
    @peer_price = PeerPrice.find(params[:id])
  end
  
  def update_model_no    
    @peer_price = PeerPrice.find(params[:id])
    render :edit_model_no if not @peer_price.update_attributes(params["peer_price_#{@peer_price.id}"])
  end
  
  def show_status
    @peer_price = PeerPrice.find(params[:id])
  end
  
  def edit_status
    @peer_price = PeerPrice.find(params[:id])
  end
  
  def update_status    
    @peer_price = PeerPrice.find(params[:id])
    render :edit_status if not @peer_price.update_attributes(params["peer_price_#{@peer_price.id}"])
  end
  
  def show_stock_situation
    @peer_price = PeerPrice.find(params[:id])
  end
  
  def edit_stock_situation
    @peer_price = PeerPrice.find(params[:id])
  end
  
  def update_stock_situation    
    @peer_price = PeerPrice.find(params[:id])
    render :edit_stock_situation if not @peer_price.update_attributes(params["peer_price_#{@peer_price.id}"])
  end
  
  def edit_peer_prices
  end
  
  def update_peer_prices
    logger.debug "开始批量修改同行价。。。"
    PeerPrice.where( 
      :enterprise_id => Enterprise.current,
      :type_name => params[:type_name],
      :released_on => params[:released_on]
    ).each do |peer_price|
      peer_price.update_attributes :peer_price => instance_eval("#{peer_price.peer_price}#{params[:peer_prices_expression]}")
    end
  end
  
  def edit_trade_prices
  end
  
  def update_trade_prices
    logger.debug "开始批发价。。。"
    PeerPrice.where( 
      :enterprise_id => Enterprise.current,
      :type_name => params[:type_name],
      :released_on => params[:released_on]
    ).each do |peer_price|
      peer_price.update_attributes :trade_price => instance_eval("#{peer_price.trade_price}#{params[:trade_prices_expression]}")
    end
  end
  
  def edit_retail_prices
  end
  
  def update_retail_prices
    logger.debug "开始批量修改零售价。。。"
    PeerPrice.where( 
      :enterprise_id => Enterprise.current,
      :type_name => params[:type_name],
      :released_on => params[:released_on]
    ).each do |peer_price|
      peer_price.update_attributes :retail_price => instance_eval("#{peer_price.retail_price}#{params[:retail_prices_expression]}")
    end
  end
  
  def destroy
    @peer_price = PeerPrice.find(params[:id])
    @peer_price.destroy
  end
  
  def new_from_excel
    
  end

  def create_from_excel
    params[:excel].split("\r\n").each_with_index do |peer_price_str, index|
#      logger.debug "#{index}. #{str}"
      attrs = peer_price_str.split("\t")
      if attrs.size >= 5
        PeerPrice.create(
          :type_name => params[:type_name],
          :name => attrs[0],
          :model_no => attrs[1],
          :peer_price => attrs[2],
          :trade_price => attrs[3],
          :retail_price => attrs[4],
          :released_on => Date.today
        )
      end
    end
  end

  def clone_from_last_day
    last_day = PeerPrice.where(
      "enterprise_id = ? and released_on < ? and type_name = ?",
      Enterprise.current,
      Date.today,
      params[:type_name]).first.try(:released_on)
    PeerPrice.where(
      "enterprise_id = ? and released_on = ? and type_name = ?",
      Enterprise.current, 
      last_day,
      params[:type_name]).each do |peer_price|
      PeerPrice.create(
        :type_name => peer_price.type_name,
        :name => peer_price.name,
        :model_no => peer_price.model_no,
        :peer_price => peer_price.peer_price,
        :trade_price => peer_price.trade_price,
        :retail_price => peer_price.retail_price,
        :released_on => Date.today,
        :stock_situation => peer_price.stock_situation,
        :status => peer_price.status
      )
    end
    redirect_to enterprise_peer_prices_url(:enterprise_id => Enterprise.current,:type_name => params[:type_name])
  end
  
  def to_new
  end
end
